package com.spring.boot.rabbit.configure.rabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

import static com.spring.boot.rabbit.constant.RoutingKey.*;

/**
 * RabbitMQ-消息队列与交换机绑定配置类
 *
 * @author 李坤
 * @date 2022/9/1 11:40
 */
@Configuration
public class BindingConfigure {

    @Resource
    private Queue defaultQueue;
    @Resource
    private Queue topicQueueSecondary;
    @Resource
    private Queue defaultDeadLetterQueue;

    @Resource
    private Exchange topicExchange;
    @Resource
    private Exchange directExchange;

    /**
     * 绑定指定的消息队列和交换机
     *
     * @return {@link Binding} 对象
     */
    @Bean
    public Binding bindDirectQueue() {
        return BindingBuilder.bind(defaultQueue).to(directExchange).with(TRADING).noargs();
    }

    /**
     * 绑定指定的消息队列和交换机
     *
     * @return {@link Binding} 对象
     */
    @Bean
    public Binding bindTopicQueue() {
        return BindingBuilder.bind(defaultQueue).to(topicExchange).with(TRADING + ".#").noargs();
    }

    /**
     * 绑定指定的消息队列和交换机
     *
     * @return {@link Binding} 对象
     */
    @Bean
    public Binding bindTopicQueueSecondary() {
        return BindingBuilder.bind(topicQueueSecondary).to(topicExchange).with(TRADING_ACCOUNT).noargs();
    }

    /**
     * 绑定指定的消息队列和交换机
     *
     * @return {@link Binding} 对象
     */
    @Bean
    public Binding bindDirectDealLetterQueue() {
        return BindingBuilder.bind(defaultDeadLetterQueue).to(directExchange).with(TRADING_DEAD_LETTER).noargs();
    }

}
